/**
 * Copyright ©2003~2019年4月7日 szkingdom
 * @author raohj
 * @date 2019年4月7日
 * @file RSAX.java
 * @comment : please input your comment to this file
 * @version : input version
 * @modify:
 * 		1、 2019年4月7日 由 raohj 新建文件
 */
package com.ccnu.demojava.xml;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;
/**
 * @author raohj
 * @date 2019年4月7日
 * @version 
 * @comment to add your comment
 */
public class RSAX {
	private static Logger log = LoggerFactory.getLogger(RSAX.class);
	public static final String XML_PATH = "D:/temp/tmp/1.xml";

	/**
	 * @author raohj
	 * @date 2019年3月30日
	 * @comment 测试SAX解析XML文件
	 * @param 
	 * @return 
	 */
	public static void SAX() {
		SAXParserFactory spf = SAXParserFactory.newInstance();
//		System.out.println(spf.getFeature(name));
		try {
			SAXParser sp = spf.newSAXParser();
			File f = new File(XML_PATH);
			sp.parse(f, new RSAX.RSAXHandler());
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static class RSAXHandler extends DefaultHandler{

		@Override
		public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException {
			// TODO Auto-generated method stub
			return super.resolveEntity(publicId, systemId);
		}

		@Override
		public void notationDecl(String name, String publicId, String systemId) throws SAXException {
			// TODO Auto-generated method stub
			super.notationDecl(name, publicId, systemId);
		}

		@Override
		public void unparsedEntityDecl(String name, String publicId, String systemId, String notationName)
				throws SAXException {
			// TODO Auto-generated method stub
			super.unparsedEntityDecl(name, publicId, systemId, notationName);
		}

		@Override
		public void setDocumentLocator(Locator locator) {
			// TODO Auto-generated method stub
			super.setDocumentLocator(locator);
		}

		@Override
		public void startDocument() throws SAXException {
			log.info("开始解析文档");
			super.startDocument();
		}

		@Override
		public void endDocument() throws SAXException {
			log.info("结束文档解析");
			super.endDocument();
		}

		@Override
		public void startPrefixMapping(String prefix, String uri) throws SAXException {
			// TODO Auto-generated method stub
			super.startPrefixMapping(prefix, uri);
		}

		@Override
		public void endPrefixMapping(String prefix) throws SAXException {
			// TODO Auto-generated method stub
			super.endPrefixMapping(prefix);
		}

		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes)
				throws SAXException {
			log.info("开始解析Element: NodeName[" +  qName + "]");
			super.startElement(uri, localName, qName, attributes);
		}

		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			log.info("结束解析Element: NodeName[" +  qName + "]");
			super.endElement(uri, localName, qName);
		}

		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
			log.info("开始解析节点值 : text[" + new String(ch, start, length));
			super.characters(ch, start, length);
		}

		@Override
		public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
			// TODO Auto-generated method stub
			super.ignorableWhitespace(ch, start, length);
		}

		@Override
		public void processingInstruction(String target, String data) throws SAXException {
			// TODO Auto-generated method stub
			super.processingInstruction(target, data);
		}

		@Override
		public void skippedEntity(String name) throws SAXException {
			// TODO Auto-generated method stub
			super.skippedEntity(name);
		}

		@Override
		public void warning(SAXParseException e) throws SAXException {
			// TODO Auto-generated method stub
			super.warning(e);
		}

		@Override
		public void error(SAXParseException e) throws SAXException {
			log.info("解析出错error");
			super.error(e);
		}

		@Override
		public void fatalError(SAXParseException e) throws SAXException {
			log.info("解析出错fatalError");
			super.fatalError(e);
		}
	}
}
